<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use app\admin\controller\Ajax;
use think\DB;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Store extends Backend
{

    //商店列表
    public function index(){
        //权限条件
        $store_name=$this->request->param('store_name');
        $where=array();
        if($store_name)
        {
            $where['s.store_name']=array('like','%'.$store_name."%");
        }
        $where['l.state']=1;
        $list = Db::name('Store')->alias('s')->join('level l','s.level = l.level')->where($where)->order('time desc')->field('s.*,l.name')->paginate(10);
        if($list->toArray())
        {
            foreach ($list as $k=>$v) {
                $data = $v;
                if ($v['state'] == 1)
                {
                    $data['state_name']='启用';
                }else{
                    $data['state_name']='禁用';
                }
                if($v['ownerid'])
                {
                    $cause['id']=$v['ownerid'];
                    $user=DB::name('user')->where($cause)->field('nickname')->find();
                    $data['nickname']=$user['nickname'];
                }else{
                    $data['nickname']="暂未分配";
                }
                $list->offsetSet($k,$data);
            }
        }
        $list->appends(['store_name'=>$store_name]);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        return $this->fetch();
    }
    public function add(){
        $level=DB::name('level')->where(array('state'=>1))->select();
        $classify=DB::name('classify')->where(array('state'=>1))->select();
        $provinces=Db::name('provinces')->select();
        $this->assign('provinces',$provinces);
        $this->assign('level',$level);
        $this->assign('classify',$classify);
        return $this->fetch();
    }
    /*市查询*/
    public function city()
    {
        $where['provinceid']=$_POST['provinces'];
        $city=DB::name('cities')->where($where)->select();
        $data['cityid']=0;
        $data['city']='请选择';
        $cities="";
        foreach ($city as $k=>$v)
        {
            $cities[0]=$data;
            $cities[$k+1]=$v;
        }
        $this->success($cities);
    }
    /*县查询*/
    public function areas()
    {
        $where['cityid']=$_POST['city'];
        $areas=DB::name('areas')->where($where)->select();
        $this->success($areas);
    }
    public function addPost(){
        $res=$_POST;
        $date['store_name']=$res['store_name'];
        $date['address']=$res['address'];
        $date['price']=$res['price'];
        $date['mav']=$res['mav'];
        $date['mobile']=$res['mobile'];
        $date['s_time']=$res['s_time'];
        $date['e_time']=$res['e_time'];
        $date['level']=$res['level'];
        $date['cat_store']=$res['cat_store'];
        $date['provinceid']=$res['provinceid'];
        $date['cityid']=$res['cityid'];
        $date['areaid']=$res['areaid'];
        $date['content']=$res['content'];
        $date['course']=$res['course'];
        $date['pingimages']=$res['img'].','.$res['img2'].','.$res['img2'];
        $date['time']=time();
        $date['state']=2;
        $store=DB::name('store')->insertGetId($date);
        if($store)
        {
            return $this->success('店铺添加成功','store/index');
        }else{
            $this->error('店铺添加失败');
        }

    }
    /*图片回传*/
    public function img()
    {
        $ajax=new Ajax();
        $file = $this->request->file('file');
        $name=$ajax->upload($file);
        return $this->success($name);
    }
    public function dateil(){
        $id  = $this->request->param('id');
        $info=Db::name('Store')->where(array('id'=>$id))->find();
        $classify=DB::name('classify')->where(array('state'=>1))->select();
        $provinces=Db::name('provinces')->select();
        $city=Db::name('cities')->select();
        $areas=Db::name('areas')->select();
        $level=DB::name('level')->select();
        $this->assign('city',$city);
        $this->assign('areas',$areas);
        $this->assign('provinces',$provinces);
        $this->assign('classify',$classify);
        $this->assign("level",$level);
        $this->assign("info", $info);
        return $this->fetch('store/detail');

    }
    public function editPost(){
        $res  =$_POST;
        $where['id']=$res['id'];
        $date['store_name']=$res['store_name'];
        $date['address']=$res['address'];
        $date['price']=$res['price'];
        $date['mav']=$res['mav'];
        $date['mobile']=$res['mobile'];
        $date['s_time']=$res['s_time'];
        $date['e_time']=$res['e_time'];
        $date['level']=$res['level'];
        $date['cat_store']=$res['cat_store'];
        $date['provinceid']=$res['provinceid'];
        $date['cityid']=$res['cityid'];
        $date['areaid']=$res['areaid'];
        $date['content']=$res['content'];
        $date['course']=$res['course'];
        $re=DB::name('store')->where($where)->update($date);
        if($re)
        {
            return $this->success('修改成功','store/index');
        }else{
            return $this->error('修改成功');
        }
    }
    function delete(){
        $id = $this->request->param("id");
        //判断这个类别下有没有店铺
        $checkM=Db::name('Member')->where(array('store_id'=>$id))->find();
        $checkU=Db::name('User')->where(array('store_id'=>$id))->find();
        if($checkM || $checkU){
            $this->error('该店铺下面有会员或店员，不能删除');
            exit;
        }
        $info = Db::name('Store')->where(['id' => $id])->find();
        if ($info){
            $status = Db::name('Store')->delete($id);
            if (!empty($status)) {
                $this->success("删除成功！", url('Store/index'));
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("该店铺不存在！");
        }
    }
    public function agree(){
        $id=$this->request->param('id');
        $check=Db::name('Store')->where(array('id'=>$id))->find();
        if($check){
            $re=Db::name('Store')->where(array('id'=>$id))->update(['jtime' =>time(), 'status' => 1]);
            if($re){
                $this->success('审核通过');
            }else{
                $this->error('审核失败');
            }
        }else{
            $this->error('此店铺不存在');
        }
    }
    public function update()
    {
        $array = $this->request->param();
        $where['id'] = $array['id'];
        $data['state'] = $array['status'];
        $state = DB::name('store')->where($where)->update($data);
        if($state)
        {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }
}
